env.GIT_REPO_URL = 'http://192.168.2.95:3000/EOM2/eom-server.git'
env.GIT_CERT_ID = 'chenwei'

pipeline {
    agent any
    
    tools {
        maven 'maven3.9'
        jdk 'JDK1.8'
    }
    
    // parameters {
    //     string(
    //         name: 'repo_url',
    //         defaultValue: 'http://192.168.2.95:3000/EOM2/eom-server.git',
    //         description: 'Git仓库地址'
    //     )
        
    //     gitParameter(
    //         name: 'branch_name',
    //         type: 'PT_BRANCH_TAG',
    //         branchFilter: 'origin/(.*)',
    //         defaultValue: 'master',
    //         selectedValue: 'DEFAULT',
    //         sortMode: 'DESCENDING_SMART',
    //         useRepository: 'http://192.168.2.95:3000/EOM2/eom-server.git',
    //         description: 'Git分支'
    //     )
    // }
    
    environment {
        git_cert_id = "chenwei"
        code_subdirectory = "./"
        git_backend_repo_url = "http://192.168.2.95:3000/EOM2/eom-server.git"
    }
    
    stages {
        stage("环境初始化") {
            steps {
                script {
                    def java_version = sh(script:'java -version 2>&1 | grep version',returnStdout:true).trim()
                    echo "Java版本:${java_version}"
                    echo "前端工程:${git_frontend_repo_url}"
                    echo "后端工程:${git_backend_repo_url}"
                    echo "源码分支:${branch}"
                    echo "镜像版本:${version}"
                
                    echo "项目构建开始"
                }
            }    
        }
        
        // stage("Git分支检出") {
        //     steps {
        //         script {
        //             println("${branch_name}")
        //             checkout(
        //                 [
        //                     $class: 'GitSCM',
        //                     branches: [[name: "${branch_name}"]],
        //                     doGenerateSubmoduleConfigurations: false, 
        //                     extensions: [[$class: 'RelativeTargetDirectory', 
        //                     relativeTargetDir: "${code_subdirectory}"]],
        //                     submoduleCfg: [], 
        //                     userRemoteConfigs: [[
        //                         credentialsId: "${git_cert_id}", 
        //                         url: "${repo_url}"
        //                     ]]
        //                 ]
        //             )
        //         }
        //     }
        // }
        
        stage("拉取仓库源码") {
            steps {
                echo "开始拉取源码"
                // git branch:'zhejiang_hospital2', credentialsId: 'chenwei', url:'http://192.168.2.95:3000/EOM2/eom-server.git'
                git branch:"${branch}", credentialsId: "chenwei", url:"http://192.168.2.95:3000/EOM2/eom-server.git"
                echo "代码拉取成功"
            }
        }
        
        stage("工程编译打包") {
            steps {
                sh """
                    echo "开始编译项目"
                    mvn clean package -Dmaven.test.skip
                    echo "项目编译成功"
                """
            }
        }
        
        stage("清空构建环境") {
            steps {
                sshPublisher(
                    publishers: [
                        sshPublisherDesc(
                            configName: 'docker.szjingshuo.top', 
                            transfers: [
                                sshTransfer(
                                    cleanRemote: false, 
                                    excludes: '', 
                                    execCommand: '''
                                        sudo rm -rf /tmp/eom-app-{admin,api,uploader}
                                        sudo docker rmi eom-admin-${branch}:${version} eom-api-${branch}:${version} eom-uploader-${branch}:${version}
                                    ''', 
                                    execTimeout: 120000, 
                                    flatten: false, 
                                    makeEmptyDirs: false, 
                                    noDefaultExcludes: false, 
                                    patternSeparator: '[, ]+', 
                                    remoteDirectory: '', 
                                    remoteDirectorySDF: false, 
                                    removePrefix: '', 
                                    sourceFiles: '/tmp'
                                )
                            ], 
                            usePromotionTimestamp: false, 
                            useWorkspaceInPromotion: false, 
                            verbose: false
                        )
                    ]
                )
            }
        }
        
        stage("构建容器镜像") {
            steps {
                echo "开始构建应用镜像"
                sshPublisher(
                    publishers: [
                        sshPublisherDesc(
                            configName: 'docker.szjingshuo.top', 
                            transfers: [
                                sshTransfer(
                                    cleanRemote: false, 
                                    execTimeout: 120000, 
                                    flatten: false, 
                                    makeEmptyDirs: false, 
                                    noDefaultExcludes: false, 
                                    patternSeparator: '[, ]+', 
                                    remoteDirectory: '/tmp/eom-app-admin', 
                                    remoteDirectorySDF: false, 
                                    removePrefix: 'eom-app-admin/target', 
                                    sourceFiles: 'eom-app-admin/target/*.jar'
                                ), 
                                sshTransfer(
                                    cleanRemote: false, 
                                    execCommand: '''
                                        docker build -t reg.szjingshuo.top/eom/eom-admin-${branch}:${version} /tmp/eom-app-admin
                                        docker push reg.szjingshuo.top/eom/eom-admin-${branch}:${version}
                                    ''', 
                                    execTimeout: 120000, 
                                    flatten: false, 
                                    makeEmptyDirs: false, 
                                    noDefaultExcludes: false, 
                                    patternSeparator: '[, ]+', 
                                    remoteDirectory: '/tmp/', 
                                    remoteDirectorySDF: false, 
                                    sourceFiles: 'eom-app-admin/Dockerfile'
                                ),
                                sshTransfer(
                                    cleanRemote: false, 
                                    execTimeout: 120000, 
                                    flatten: false, 
                                    makeEmptyDirs: false, 
                                    noDefaultExcludes: false, 
                                    patternSeparator: '[, ]+', 
                                    remoteDirectory: '/tmp/eom-app-api', 
                                    remoteDirectorySDF: false, 
                                    removePrefix: 'eom-app-api/target', 
                                    sourceFiles: 'eom-app-api/target/*.jar'
                                ), 
                                sshTransfer(
                                    cleanRemote: false, 
                                    execCommand: '''
                                        docker build -t reg.szjingshuo.top/eom/eom-api-${branch}:${version} /tmp/eom-app-admin
                                        docker push reg.szjingshuo.top/eom/eom-api-${branch}:${version}
                                    ''', 
                                    execTimeout: 120000, 
                                    flatten: false, 
                                    makeEmptyDirs: false, 
                                    noDefaultExcludes: false, 
                                    patternSeparator: '[, ]+', 
                                    remoteDirectory: '/tmp/', 
                                    remoteDirectorySDF: false, 
                                    sourceFiles: 'eom-app-api/Dockerfile'
                                ),
                                sshTransfer(
                                    cleanRemote: false, 
                                    execTimeout: 120000, 
                                    flatten: false, 
                                    makeEmptyDirs: false, 
                                    noDefaultExcludes: false, 
                                    patternSeparator: '[, ]+', 
                                    remoteDirectory: '/tmp/eom-app-uploader', 
                                    remoteDirectorySDF: false, 
                                    removePrefix: 'eom-app-uploader/target', 
                                    sourceFiles: 'eom-app-uploader/target/*.jar'
                                ), 
                                sshTransfer(
                                    cleanRemote: false, 
                                    execCommand: '''
                                        docker build -t reg.szjingshuo.top/eom/eom-uploader-${branch}:${version} /tmp/eom-app-uploader
                                        docker push reg.szjingshuo.top/eom/eom-uploader-${branch}:${version}
                                    ''',                                     
                                    execTimeout: 120000, 
                                    flatten: false, 
                                    makeEmptyDirs: false, 
                                    noDefaultExcludes: false, 
                                    patternSeparator: '[, ]+', 
                                    remoteDirectory: '/tmp/', 
                                    remoteDirectorySDF: false, 
                                    sourceFiles: 'eom-app-uploader/Dockerfile'
                                )
                            ], 
                            usePromotionTimestamp: false, 
                            useWorkspaceInPromotion: false, 
                            verbose: true
                        )
                    ]
                )
                echo "应用镜像构建完毕"
            }
        }
        
        stage("构建前端工程") {
            
        }
        
        stage("清除构建缓存") {
            steps {
                echo "开始清除构建缓存"
                sshPublisher(
                    publishers: [
                        sshPublisherDesc(
                            configName: 'docker.szjingshuo.top', 
                            transfers: [
                                sshTransfer(
                                    cleanRemote: false, 
                                    excludes: '', 
                                    execCommand: '''
                                        sudo rm -rf /tmp/eom-app-{admin,api,uploader}
                                        sudo docker image prune -f
                                    ''', 
                                    execTimeout: 120000, 
                                    flatten: false, 
                                    makeEmptyDirs: false, 
                                    noDefaultExcludes: false, 
                                    patternSeparator: '[, ]+', 
                                    remoteDirectory: '', 
                                    remoteDirectorySDF: false, 
                                    removePrefix: '', 
                                    sourceFiles: '/tmp'
                                )
                            ], 
                            usePromotionTimestamp: false, 
                            useWorkspaceInPromotion: false, 
                            verbose: false
                        )
                    ]
                )
                echo "构建缓存清除完毕"
                
                 echo "项目构建完成"
            }
        }
    }
}
